package com.artisan.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import com.artisan.dao.CourseDao;
import com.artisan.dao.ScoreDao;
import com.artisan.dao.SelectedCourseDao;
import com.artisan.model.Course;
import com.artisan.model.Score;
import com.artisan.model.SelectedCourse;
import com.artisan.model.Student;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class ViewscoreFrm extends JInternalFrame {
	private JTable scoreListTable;
	private JLabel studentNameLabel;
	private JComboBox courseComboBox;
	private List<Course> courseList = new ArrayList<Course>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ViewscoreFrm frame = new ViewscoreFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ViewscoreFrm() {
		setClosable(true);
		setIconifiable(true);

		getContentPane().setFont(new Font("微软雅黑", Font.PLAIN, 14));
		setFrameIcon(new ImageIcon(ViewscoreFrm.class.getResource("/com/artisan/image/搜索.png")));
		setTitle("学生成绩查询");
		setBounds(100, 100, 711, 459);

		JLabel lblNewLabel = new JLabel("学生姓名:");
		lblNewLabel.setIcon(new ImageIcon(ViewscoreFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JLabel lblNewLabel_1 = new JLabel("所选课程:");
		lblNewLabel_1.setIcon(new ImageIcon(ViewscoreFrm.class.getResource("/com/artisan/image/课程.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		studentNameLabel = new JLabel("");
		studentNameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		courseComboBox = new JComboBox();
		courseComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				courseChangedAct(ie);
			}
		});

		JScrollPane scrollPane = new JScrollPane();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(96).addComponent(lblNewLabel).addGap(18)
						.addComponent(studentNameLabel, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
						.addGap(38).addComponent(lblNewLabel_1).addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(courseComboBox, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(29, Short.MAX_VALUE))
				.addGroup(Alignment.TRAILING,
						groupLayout.createSequentialGroup().addContainerGap(27, Short.MAX_VALUE)
								.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 646, GroupLayout.PREFERRED_SIZE)
								.addGap(26)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
				.createSequentialGroup().addGap(38)
				.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
						.addComponent(studentNameLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1).addComponent(courseComboBox, GroupLayout.PREFERRED_SIZE,
								GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED)
				.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE)
				.addContainerGap(115, Short.MAX_VALUE)));

		scoreListTable = new JTable();
		scoreListTable.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "\u6210\u7EE9ID",
				"\u5B66\u751F\u59D3\u540D", "\u8BFE\u7A0B\u540D\u79F0", "\u6240\u5F97\u6210\u7EE9" }) {
			boolean[] columnEditables = new boolean[] { false, false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scoreListTable.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		scrollPane.setViewportView(scoreListTable);
		getContentPane().setLayout(groupLayout);
		setCourseCombox();
		initTable();
	}

	protected void courseChangedAct(ItemEvent ie) {
		// TODO 自动生成的方法存根
		setTable();
	}

	private void setCourseCombox() {
		CourseDao courseDao = new CourseDao();
		courseList = courseDao.getCourseList(new Course());
		courseDao.closeDao();
		Student student = (Student) MainFrm.userObject;
		studentNameLabel.setText(student.getName());
		SelectedCourse sc = new SelectedCourse();
		sc.setStudent_id(student.getId());
		SelectedCourseDao scDao = new SelectedCourseDao();
		List<SelectedCourse> selectedCourseList = scDao.getSelectedCourseList(sc);
		for (SelectedCourse selectedCourse : selectedCourseList) {
			courseComboBox.addItem(getCourseById(selectedCourse.getCourse_id()));
		}
	}

	private void initTable() {
		Student student = (Student) MainFrm.userObject;
		Score score = new Score();
		score.setStudent_id(student.getId());
		getScoreList(score);
	}

	private Course getCourseById(int id) {
		for (int i = 0; i < courseList.size(); i++) {
			if (id == courseList.get(i).getId()) {
				return courseList.get(i);
			}
		}
		return null;
	}

	private void getScoreList(Score score) {
		Student student = (Student) MainFrm.userObject;
		ScoreDao scoreDao = new ScoreDao();
		List<Score> scoreList = scoreDao.getScoreList(score);
		DefaultTableModel dft = (DefaultTableModel) scoreListTable.getModel();
		dft.setRowCount(0);
		for (Score s : scoreList) {
			Vector v = new Vector();
			v.add(s.getId());
			v.add(student.getName());
			v.add(getCourseById(s.getCourse_id()));
			v.add(s.getScore());
			dft.addRow(v);
		}
		scoreDao.closeDao();
	}

	private void setTable() {
		Student student = (Student) MainFrm.userObject;
		Course course = (Course) courseComboBox.getSelectedItem();
		Score score = new Score();
		score.setStudent_id(student.getId());
		score.setCourse_id(course.getId());
		getScoreList(score);
	}
}
